package naga.x.platform2.app;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import naga.x.platform2.app.model.GameServer;
import naga.x.platform2.app.model.Recharge;
import naga.x.platform2.app.model.RechargeTemplate;
import naga.x.platform2.app.support.GameServerSupport;
import naga.x.platform2.app.support.RechargeSupport;
import naga.x.platform2.support.RequestPath;

@Controller
public class AppController {

	@Autowired
	private GameServerSupport gameServers;
	@Autowired
	private RechargeSupport recharges;
	
	@RequestMapping(path = RequestPath.SERVER_LIST, produces = "application/json; charset=UTF-8")
	@ResponseBody
	public Object requestServerList(@PathVariable String game,
			@RequestParam(name = "cid", required = false) String channelId, String uid) {
		Channel channel = Channel.forCode(channelId);
		if (channel == Channel.UNKNOWN) {
			channel = Channel.OFFICIAL;
		}
		if (channel.debug) {
			return Arrays.asList(gameServers.get(1001)); // 测试服
		}
		return gameServers.get(channel, uid);
	}
	
	@RequestMapping(path = RequestPath.SERVER_LIST_TEST, produces = "application/json; charset=UTF-8")
	@ResponseBody
	public Object requestTestServerList(@PathVariable String game) {
		return gameServers.getAll();
	}
	
	@RequestMapping(path = RequestPath.FAKE_RECHARGE)
	@ResponseBody
	public String requestFakeRecharge(Integer serverId, Integer productId, String password) {
		if (serverId == null || productId == null || password == null) {
			return "param error";
		}
		GameServer gs = gameServers.get(serverId);
		if (gs == null || !gs.isTest()) {
			return "no permission";
		}
		RechargeTemplate template = recharges.getTemplate(productId);
		if (template == null) {
			return "product error";
		}
		String orderId = "test:" + System.currentTimeMillis();
		Recharge recharge = new Recharge(orderId, 0L, serverId, template.getPrice(), template.getStone(), productId, Channel.OFFICIAL.name);
		recharge.setChannelUid(password);
		if (recharges.offer(recharge)) {
			return "success";
		}
		return "duplicate";
	}
	
	@RequestMapping(path = RequestPath.SERVER_LIST_PLAIN, produces = "text/html; charset=UTF-8")
	@ResponseBody
	public String requestServerListPlain(Boolean test) {
//		<html>
//		<head>
//		<meta charset="utf-8">
//		</head>
//		<body>
//		  <table>
//		    <tr>
//		      <th>id</th>
//			  <th>服务器</th>
//			  <th>渠道</th>
//			  <th>地址</th>
//			  <th>直连端口</th>
//			  <th>代理端口</th>
//		    </tr>
//			<tr>
//			  <td>...</td>
//			</tr>
//		  </table>
//		</body>
//		</html>
		StringBuilder builder = new StringBuilder(1024);
		builder.append("<html><head><meta charset=\"utf-8\"></head><body><table border=\"1\"><tr><th>id</th><th>服务器</th><th>渠道</th><th>地址</th><th>直连端口</th><th>代理端口</th></tr>");
		List<GameServer> list = test != null && test ? gameServers.getTestServers() : gameServers.getServers();
		for (GameServer gs : list) {
			builder.append("<tr>");
			builder.append("<td>").append(gs.getId()).append("</td>");
			builder.append("<td>").append(gs.getName()).append("</td>");
			builder.append("<td>");
			for (Channel c : gs.getChannels()) {
				builder.append(c.displayName).append(',');
			}
			builder.setLength(builder.length() - 1);
			builder.append("</td>");
			builder.append("<td>").append(gs.getHost()).append("</td>");
			builder.append("<td>").append(gs.getPort()).append("</td>");
			builder.append("<td>");
			if (gs.getProxyPorts() != null) {
				for (int pp : gs.getProxyPorts()) {
					builder.append(pp).append(',');
				}
			}
			builder.setLength(builder.length() - 1);
			builder.append("</td>");
			builder.append("</tr>");
		}
		builder.append("</table></body></html>");
		return builder.toString();
	}
	
}
